import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/users/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Categories from '../components/goods/Cate.vue'
//导入参数列表组件
import Params from '../components/goods/Params.vue'
//导入列表组件
import List from '../components/goods/List.vue'
//导入添加商品组件
import Add from '../components/goods/Add.vue'
//导入添加订单组件
import Order from '../components/order/Order.vue'
//导入添加数据组件
import Report from '../components/report/Report.vue'
Vue.use(VueRouter)

const routes = [
  {
	  path:'/login',
	  component:Login
  },
  {
	  path:'/',
	  redirect:'/login'
  },
  {
  	  path:'/home',
  	  component:Home,
	  //先把home组件渲染出来了。再通过路由占位符渲染其中的子组件welcome
	  redirect:'/welcome',
	  children:[
		  {
		  	  path:'/welcome',
		  	  component:Welcome
		  },
		  {
		  	  path:'/users',
		  	  component:Users
		  },
		  {
		  	  path:'/rights',
		  	  component:Rights
		  },
		  {
		  	  path:'/roles',
		  	  component:Roles
		  },
		  {
		  	  path:'/categories',
		  	  component:Categories
		  },
		  {path:'/params',component:Params},
		  {path:'/goods',component:List},
		  {path:'/goods/add',component:Add},
		  {path:'/orders',component:Order},
		  {path:'/reports',component:Report}
	  ]
  },
]

const router = new VueRouter({
  routes
})

//挂载全局路由导航守卫，需要由登陆的token才能访问页面
router.beforeEach((to,from,next)=>{
	//若访问的是登陆界面，则放行
	if (to.path === '/login') return next()
	const tokenStr=window.sessionStorage.getItem('token')
	//若token不存在，强制登陆
	if(!tokenStr) return next('/login')
	//token存在，则放行
	next()
})

export default router
